package com.gettec.lims.client.forms;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.binding.FormBinding;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.BeanModelLookup;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonBar;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.FormPanel.LabelAlign;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.ColumnData;
import com.extjs.gxt.ui.client.widget.layout.ColumnLayout;
import com.extjs.gxt.ui.client.widget.layout.FlowData;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.gettec.lims.client.bind.DateFieldBinding;
import com.gettec.lims.client.bind.RadioGroupBinding;
import com.gettec.lims.client.services.NnimsServiceAsync;
import com.gettec.lims.shared.model.RegSheet;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class RegSheetForm  extends Dialog
{
	protected Button reset;
	protected Button save;
	protected FormPanel form ;
	protected ButtonBar buttonBar;
	protected BeanModel regSheetModel;
	protected NnimsServiceAsync regSheetService;
	protected ListStore<BeanModel> store;
	//private final TextField<String> reg_number = new TextField<String>();  
	private final TextField<String> data_scope = new TextField<String>();  
	private final TextField<String> client_org = new TextField<String>();
	private final DateField reg_date = new DateField();   
	private final DateField limit_date = new DateField();   
	private final TextField<String> under_dept = new TextField<String>();
	private final TextField<String> bussiness_type = new TextField<String>();  
	private final TextField<String> area_info = new TextField<String>();  
	private final TextField<String> detect_type = new TextField<String>();
	private final TextField<String> detect_way = new TextField<String>();  
	private final TextField<String> report_send_way = new TextField<String>();   
	private final RadioGroup issubproject = new RadioGroup();  
	private final RadioGroup isconclusion_req = new RadioGroup(); 
	private final RadioGroup issubcontract = new RadioGroup(); 
	private final TextField<String> pay_type = new TextField<String>();  
	private final TextField<String> client_req = new TextField<String>();
	private final TextField<String> service_declare = new TextField<String>();  
	private final TextField<String> accepter = new TextField<String>();  
	private final TextField<String> client = new TextField<String>();
	public RegSheetForm(){}
	Field[] list={
	data_scope,client_org,reg_date,limit_date,under_dept,
			bussiness_type,area_info,detect_type,detect_way,report_send_way,issubproject,
			isconclusion_req,issubcontract,pay_type,client_req,service_declare,accepter,client};
	String[] captions={
"data_scope","client_org","reg_date","limit_date","under_dept","business_type","area_info","detect_type",
"detect_way","report_send_way","issubproject","isconclusion_req","issubcontract","pay_type","client_req","service_declare","accepter,","client"};
	String[] zh_captions={
			"数据涵盖范围","委托单位","委托登记日期","规定完成日期","所属部门","业务类别","地区信息","检测类别",
			"委托检测方法","报告发送方式","分包项目","结论要求","报告是否明示分包","付款方式","委托要求","服务方申明",
			"受理人,","委托人"};
	//{reg_number,data_scope,entrust_org,reg_date,limit_date,under_dept,bussiness_type,area_info,detect_type,detect_way,report_send_way,issubproject,
		//isconclusion_req,pay_type,entrust_reg,service_declear,accepter,entruster};
	public RegSheetForm(final NnimsServiceAsync regSheetService, final ListStore<BeanModel> store,final RegSheet regSheet)
	{
		setWidth(600);
		this.regSheetService=regSheetService;
		this.store=store;
		FormData formData = new FormData("100%");   
	    form = new FormPanel();   
	    form.setFrame(true);   
	  
	    form.setHeading("FormPanel");   
	    form.setSize(600, -1);   
	    form.setLabelAlign(LabelAlign.TOP);   
	    form.setButtonAlign(HorizontalAlignment.CENTER);   
	  
	    LayoutContainer main = new LayoutContainer();   
	    main.setLayout(new ColumnLayout()); 
	    LayoutContainer left = new LayoutContainer();   
	    left.setStyleAttribute("paddingRight", "10px");   
	    FormLayout layout = new FormLayout();   
	    layout.setLabelAlign(LabelAlign.TOP);   
	    left.setLayout(layout);   
	    
	    LayoutContainer right = new LayoutContainer();   
	    right.setStyleAttribute("paddingLeft", "10px");   
	    layout = new FormLayout();   
	    layout.setLabelAlign(LabelAlign.TOP);   
	    right.setLayout(layout);   
//---------------------------------------------------------------
	    Radio issubproject_radio1 = new Radio();   
	    issubproject_radio1.setBoxLabel("是");   
	    Radio issubproject_radio2 = new Radio();   
	    issubproject_radio2.setBoxLabel("否");   
	    issubproject.add(issubproject_radio1);   
	    issubproject.add(issubproject_radio2); 
	    
	    Radio isconclusion_req_radio1 = new Radio();   
	    isconclusion_req_radio1.setBoxLabel("是");   
	    Radio isconclusion_req_radio2 = new Radio();   
	    isconclusion_req_radio2.setBoxLabel("否");   
	    isconclusion_req.add(isconclusion_req_radio1);   
	    isconclusion_req.add(isconclusion_req_radio2);  
	    
	    Radio issubcontract_radio1 = new Radio();   
	    issubcontract_radio1.setBoxLabel("是");   
	    Radio issubcontract_radio2 = new Radio();   
	    issubcontract_radio2.setBoxLabel("否");   
	    issubcontract.add(issubcontract_radio1);   
	    issubcontract.add(issubcontract_radio2);  
//--------------------------------------------------------------
	    
	    

		for( int i=0;i<captions.length;i++)
		{
			list[i].setName(captions[i]);
			list[i].setFieldLabel(zh_captions[i]);
			if(i%2==0)
			{
				left.add(list[i], formData);   

			}
			else
			{
				right.add(list[i], formData);   

			}
			/*list[i].setValidateOnBlur(true);
			list[i].setValidationDelay(1);
			list[i].validate(true);
			list[i].validate();*/
		}
		  main.add(left, new ColumnData(.5));   
		    main.add(right, new ColumnData(.5));   
		  
		    form.add(main, new FormData("100%")); 
		    regSheetModel = BeanModelLookup.get().getFactory(RegSheet.class).createModel(regSheet);
			final FormBinding formBinding = new FormBinding(form, true);
			formBinding.bind(regSheetModel);
			formBinding.addFieldBinding(new RadioGroupBinding(issubcontract));
			formBinding.addFieldBinding(new RadioGroupBinding(issubproject));
			formBinding.addFieldBinding(new RadioGroupBinding(isconclusion_req));
			formBinding.addFieldBinding(new DateFieldBinding(reg_date));
			formBinding.addFieldBinding(new DateFieldBinding(limit_date));
			
			add(form);
			buttonBar = new ButtonBar();
			createButton() ;
			setButtons("");//这样可以隐藏不必要的ok按钮
			 add(buttonBar, new FlowData(10));
			 
			show();		
	}
	/*public RegSheetForm()
	{
		super();
		init();
	}*/
	/*public void save(RegSheet sheet)
	{
		RegSheet rs=new RegSheet();
		for(int i=0;i<list.length;i++)
		{
			
		}
	}*/
	/*此方法先于构造方法运行，所以注意控件初始化顺序
	 * @see com.extjs.gxt.ui.client.widget.Dialog#createButtons()
	 */
	
	protected void createButton() {
		
			reset = new Button("重置");
			reset.addSelectionListener(new SelectionListener<ButtonEvent>() {
				public void componentSelected(ButtonEvent ce) {
					//CustomerDialog.this.hide();
					form.reset();
				}

			});

			save = new Button("保存");
			
			save.addSelectionListener(new SelectionListener<ButtonEvent>() {
				public void componentSelected(ButtonEvent ce) {
					if (form.isValid()) {
					
						buttonBar.disable();
						
						final RegSheet regSheet = regSheetModel.getBean();
						regSheetService.saveRegSheet(regSheet, new AsyncCallback<Integer>() {
							public void onFailure(Throwable caught) {							
								
								buttonBar.enable();	
								MessageBox.alert("提示信息", "保存失败！", null);
							}

							public void onSuccess(Integer result) {							
								
								buttonBar.enable();								
								
								store.add(regSheetModel);
								store.getLoader().load();
								
								MessageBox.alert("提示信息", "保存成功！", null);
							}						
						});
					}
				}
			});

			buttonBar.add(save);
			buttonBar.add(reset);
		}
	
	
}
